<style lang="scss" scoped>

@import "./_init.scss";
@import "./_form.scss";
.VUE{
    & > .I-loading{
        height:96px;
        width:96px
    }
}
</style>

<template lang="pug">
.VUE
    form(@submit.prevent="submit" ref="form")
        .I-loading(v-if="val===0")
        .P.TXT(v-else-if="val===false")
            p 该邮箱已经注册 
            p 
                a.button(href="/auth/login") 点此登录 
            p 
                a.button.gray(href="/auth/password/reset") 找回密码 
        .P.TXT(v-else-if="val===true")
            p 注册成功 
            p 
                a.button(href="/") 开始使用 
        .P.TXT(v-else-if="val===''")
            p 验证链接已过期失效
            p 
                a.button(:href="'/auth/'+$route.params.en+'/new'") 点此重新注册
        div(v-else)
            .P
                input(:value="val" disabled)
                label.I(:class="'I-'+$route.params.en")
            .P
                input(
                    name="name" placeholder="请输入您的姓名" v-focus="1" spellcheck="false" autocomplete="off" v-model.trim="name" required
                )
                label.I.I-user
            .P
                input(name="password" placeholder="请设置您的密码" type="password" v-model.trim="password" required)
                label.I.I-password
            p
                button(type="submit") 创建账户
            p
                a.gray.button(href="/auth/login") 放弃注册 
</template>

<script lang="coffee">
store = require 'store'
ERROR = require './_error.coffee'

export default {
    data : ->
        {
            val : 0
            password:""
            name:""
        }

    methods:

        submit:->
            {en, key} = @$route.params
            data = {
                password:@password
                name:@name
            }
            form = $(@$refs.form)
            form.save(
                "*auth/create/#{en}"
                {
                    key
                    ...data
                }
                =>
                    store.set 'account', @val
                    @val = true
                (err)=>
                    if err.user
                        return {}
                    if 'val' of err
                        @val = err.val
                        return {}
                    switch err.name
                        when ERROR.NAME.LENGTH
                            len = data.name.length
                            if len < 2
                                err.name = "名字不少于两个字符"
                            else if len > 32
                                err.name = "名字不超过三十二个字符"
                        when ERROR.NAME.FORMAT
                            err.name = "名字不能含有特殊字符"
                        when ERROR.NAME.DIGIT
                            err.name = "名字不能为纯数字"
                        when ERROR.NAME.EXIST
                            err.name = "名字已被使用（加数字后缀试试？）"
                    require('./_err/password') err, data.password
            )


    mounted:->
        {key, en} = @$route.params
        @val = await $.get("*auth/create/#{en}-"+key)
}
</script>

